﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mrc.Common;
using SqlSugar;
namespace System
{
    public class LayuiTableJson<T>
    {
        /// <summary>
        /// 返回状态码
        /// </summary>
        public int code { get; set; }

        /// <summary>
        /// 信息
        /// </summary>
        public string msg { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int count { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<T> data { get; set; }

        public LayuiTableJson()
        {
            data = new List<T>();
        }
    }

    public static class PageHelperExt2
    { 
        public static LayuiTableJson<T> ToLayuiTable<T>(this IEnumerable<T> source, int page, int pageSize, string msg = "")
        {
            LayuiTableJson<T> result = new LayuiTableJson<T>();
            result.count = source.Count();
            result.data = source.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            result.code = 0;
            result.msg = "OK";
            return result;
        }
        public static LayuiTableJson<T> ToLayuiTable<T>(this ISugarQueryable<T> source, int page, int pageSize, string msg = "")
        {
            LayuiTableJson<T> result = new LayuiTableJson<T>();
            result.count = source.Count();
            result.data = source.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            result.code = 0;
            result.msg = "OK";
            return result;
        }

        public static LayuiTableJson<T> ToTargetLayuiFlow<T, TSource>(this ISugarQueryable<TSource> source, int page, int pageSize)
        {
            LayuiTableJson<T> result = new LayuiTableJson<T>();
            result.count = source.Count();
            result.code = 0;
            result.msg = "OK";
            source.Skip((page - 1) * pageSize).Take(pageSize).ToList().ForEach(item => result.data.Add((T)MapEx.CopyModel((T)Activator.CreateInstance(typeof(T)), item)));
            return result;
        }

    }

}
